package com.chahua.organized.controller;

import com.chahua.organized.common.BaseResponseCode;
import com.chahua.organized.common.DataResult;
import com.chahua.organized.common.PageData;
import com.chahua.organized.qo.DepartmentMemberDeleteQo;
import com.chahua.organized.qo.DepartmentMemberMoveQo;
import com.chahua.organized.qo.DepartmentMemberPageQo;
import com.chahua.organized.qo.DepartmentMemberQo;
import com.chahua.organized.service.DepartmentMemberService;
import com.chahua.organized.vo.DepartmentMemberInfoVo;
import com.chahua.organized.vo.DepartmentMemberInputVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;


@Api(value = "人员管理")
@RestController
@RequestMapping("/api/v1")
public class DepartmentMemberController {

    @Autowired
    private DepartmentMemberService departmentMemberService;

    @ApiOperation(value = "新增成员")
    @PostMapping("/member")
    public DataResult addMember(@RequestBody @Valid DepartmentMemberQo departmentMemberQo) {
        departmentMemberService.add(departmentMemberQo);
        DataResult result = DataResult.success();
        return result;
    }

    @ApiOperation(value = "查询成员信息")
    @GetMapping("/member/{id}")
    public DataResult<DepartmentMemberInfoVo> getMember(@PathVariable("id") @NotNull Integer id) {
        DepartmentMemberInfoVo memberInfoVo = departmentMemberService.getMemberById(id);
        DataResult result = DataResult.success();
        result.setData(memberInfoVo);
        return result;
    }

    @ApiOperation(value = "更新成员信息")
    @PatchMapping("/member/{id}")
    public DataResult updateMember(@PathVariable("id") @NotNull Integer id, @RequestBody @Valid DepartmentMemberQo departmentMemberQo) {
        departmentMemberService.updateMemberById(id,departmentMemberQo);
        DataResult result = DataResult.success();
        return result;
    }

    @ApiOperation(value = "查询成员列表")
    @GetMapping("/member")
    public DataResult<PageData<DepartmentMemberInfoVo>> pageQueryMemberList(@RequestBody @Validated DepartmentMemberPageQo departmentMemberPageQo) {
        PageData<DepartmentMemberInfoVo> pageData = departmentMemberService.listWithPage(departmentMemberPageQo);
        DataResult result = DataResult.success();
        result.setData(pageData);
        return result;
    }

    @ApiOperation(value = "删除成员")
    @DeleteMapping("/member")
    public DataResult deleteMember(@RequestBody DepartmentMemberDeleteQo memberDeleteQo) {
        departmentMemberService.delete(memberDeleteQo.getIds());
        return DataResult.success();
    }

    @ApiOperation(value = "移动人员")
    @PatchMapping("/member/move")
    public DataResult moveMember(@RequestBody @Valid DepartmentMemberMoveQo departmentMemberMoveQo){
        departmentMemberService.move(departmentMemberMoveQo);
        return DataResult.success();
    }

    @ApiOperation("下载excel模板")
    @GetMapping("/member/downloadTemplate")
    public DataResult downloadTemplate(HttpServletResponse response, HttpServletRequest request) throws IOException {
        departmentMemberService.download(response,request);
        return DataResult.success();
    }

    @ApiOperation("excel人户信息导入")
    @PostMapping("/member/import")
    public DataResult<DepartmentMemberInputVo> importMember(MultipartFile multipartFile, HttpServletResponse resp){
        DepartmentMemberInputVo departmentMemberInputVo = departmentMemberService.importMember(multipartFile, resp);
        DataResult result = DataResult.success();
        result.setData(departmentMemberInputVo);
        return result;
    }

}

